;;; init-window.el --- Emacs window configuration -*- lexical-binding:t -*-
;;; Commentary:
;;; Code:

(defun split-window-func-with-other-buffer (split-function)
  (lambda (&optional arg)
    "Split this window and switch to the new window unless ARG is provided."
    (interactive "P")
    (funcall split-function)
    (let ((target-window (next-window)))
      (set-window-buffer target-window (other-buffer))
      (unless arg
        (select-window target-window)))))

(defun split-window-horizontally-instead ()
  "Kill any other windows and re-split such that the current window is on the top half of the frame."
  (interactive)
  (let ((other-buffer (and (next-window) (window-buffer (next-window)))))
    (delete-other-windows)
    (split-window-horizontally)
    (when other-buffer
      (set-window-buffer (next-window) other-buffer))))

(defun split-window-vertically-instead ()
  "Kill any other windows and re-split such that the current window is on the left half of the frame."
  (interactive)
  (let ((other-buffer (and (next-window) (window-buffer (next-window)))))
    (delete-other-windows)
    (split-window-vertically)
    (when other-buffer
      (set-window-buffer (next-window) other-buffer))))

(defun sanityinc/split-window()
  "Split the window to see the most recent buffer in the other window.
Call a second time to restore the original window configuration."
  (interactive)
  (if (eq last-command 'sanityinc/split-window)
      (progn
        (jump-to-register :sanityinc/split-window)
        (setq this-command 'sanityinc/unsplit-window))
    (window-configuration-to-register :sanityinc/split-window)
    (switch-to-buffer-other-window nil)))

(defun ctrlwei/toggle-maximize-buffer ()
  "Maximize buffer"
  (interactive)
  (let ((tmp-register :ctrlwei/toggle-maximize-buffer))
    (save-excursion
      (if (and (= 1 (length (window-list)))
               (assoc tmp-register register-alist))
          (jump-to-register tmp-register)
        (progn
          (window-configuration-to-register tmp-register)
          (delete-other-windows))))))



(add-hook 'after-init-hook 'winner-mode)
(global-set-key (kbd "M-q h") #'winner-undo)
(global-set-key (kbd "M-q l") #'winner-redo)



(global-set-key (kbd "C-x o") 'ace-window)
(with-eval-after-load 'ace-window
  (when (display-graphic-p)
    (ace-window-posframe-mode))

  (setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))
  (setq aw-background nil)
  (setq aw-dispatch-when-more-than 3)
  (setq aw-minibuffer-flag t))

(setq winum-keymap
    (let ((map (make-sparse-keymap)))
      ;; (define-key map (kbd "M-0") #'winum-select-window-0-or-10)
      (define-key map (kbd "M-1") #'winum-select-window-1)
      (define-key map (kbd "M-2") #'winum-select-window-2)
      (define-key map (kbd "M-3") #'winum-select-window-3)
      (define-key map (kbd "M-4") #'winum-select-window-4)
      (define-key map (kbd "M-5") #'winum-select-window-5)
      (define-key map (kbd "M-6") #'winum-select-window-6)
      (define-key map (kbd "M-7") #'winum-select-window-7)
      (define-key map (kbd "M-8") #'winum-select-window-8)
      map))
(setq winum-format "%s|")
(setq winum-mode-line-position 0)
(setq winum-scope 'frame-local)
(winum-mode 1)
(add-to-list 'winum-ignored-buffers " *corfu*")



(setq display-buffer-alist `(("^\\*eshell\\|^\\*vterm\\|^\\vterm"
                              (display-buffer-reuse-mode-window
                               display-buffer-at-bottom)
                              (mode . (eshell-mode vterm-mode))
                              (window-height . 0.4))
                             ("^\\*helpful"
                              (display-buffer-reuse-mode-window)
                              (mode . helpful-mode))
                             ("^\\*Messages\\*$\\|^\\*Process\\ List\\*$\\|^\\*Occur\\*$\\|^\\*Debug (Launch) - Integrate Terminal"
                              ,(car display-buffer-fallback-action)
                              (post-command-select-window . t))))



(setq tab-bar-close-button-show nil
      tab-bar-new-button-show nil
      tab-bar-auto-width nil
      tab-bar-show 1
      tab-bar-select-tab-modifiers '(super))
(global-set-key (kbd "s-=") #'tab-bar-new-tab)
(global-set-key (kbd "s--") #'tab-bar-close-tab)
(global-set-key (kbd "s-j") #'tab-bar-switch-to-prev-tab)
(global-set-key (kbd "s-k") #'tab-bar-switch-to-next-tab)
(global-set-key (kbd "s-r") #'tab-bar-rename-tab)




;; Key bindings
(global-set-key (kbd "M-q 1") #'ctrlwei/toggle-maximize-buffer)
(global-set-key (kbd "C-x 2") (split-window-func-with-other-buffer 'split-window-vertically))
(global-set-key (kbd "C-x 3") (split-window-func-with-other-buffer 'split-window-horizontally))
(global-set-key (kbd "C-x |") #'split-window-horizontally-instead)
(global-set-key (kbd "C-x _") #'split-window-vertically-instead)
(global-set-key (kbd "<f5>") #'sanityinc/split-window)


(provide 'init-window)
;;; init-window.el ends here
